var app = getApp();
var a=0;
 
function requestHttp(action, params, cb) {
 
  var url = app.globalData.service + action;
  if (url.search('addformid') == -1) {
    if (url.search('getBargainInfo')==-1){ 
      if(a==1){
        alertTItle();
      }
    }else{
      a=0;
      setTimeout(function(){
        a=1;
      },3000);
    }
  }
  console.log('====================================================');
  console.log(action);
  console.log(params);

  wx.request({
    url: url,
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    data: params,
    method: 'GET',
    success: function (res) {
      console.log(res);
      if (res.data.state == 0 && res.data.msg) {
        if(res.data.msg == '登录状态失效' || res.data.msg == '查无此用户或openid不符'){
          try {
            wx.removeStorageSync('user_id')
          } catch (e) {
            // Do something when catch error
          }
          try {
            wx.removeStorageSync('user_token')
          } catch (e) {
            // Do something when catch error
          }
          return
        }

        checkIfalertMsg(res);
        
        //接口针对失败要做处理
      }
      typeof cb == "function" && cb(res);

    },
    fail: function (res) {
      console.log('++++++++++++++++++++++++++++++++++++++接口失败');
      // fail
      wx.showToast({
        title: '网络请求失败，请退出重试',
        icon:'none'
      })
      return
      typeof cb == "function" && cb(null);
    },
    complete: function () {
      // console.log('接口完成--------------------');
      
    }
  });

}
function checkIfalertMsg(res){

  var is_new = wx.getStorageSync("is_new");
  if (res.data.msg == '邀请已失效' || res.data.msg == '邀请已过期') {
    if (is_new == 1) {
      //如果是新人不提示
    } else {
      if (res.data.msg) {
        // 如果不是新人,需要提示 
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          mask: true,
          complete: function () {
          },
        });
      }
    }
  } else {

    if (res.data.msg) {
      if (res.data.msg == '用户不存在') {
        //不显示提示
      } else {
        //如果提示的内容不是 邀请已失效
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          mask: true,
          complete: function () {

          },
        });
      }
    }
  }
}


function requestPostHttp(url, param, success) {
  let baseUrl = app.globalData.service + url;
  
  //如果全局user_id或user_token为0的话,就再去缓存里面去重新去一次
  if (app.globalData.user_id == 0 || app.globalData.user_token==0){
    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      console.log(userInfo.user_id)
      app.globalData.user_id = userInfo.user_id;
      app.globalData.user_token = userInfo.user_token;

      param["user_id"] = app.globalData.user_id;
      param["user_token"] = app.globalData.user_token;
    }  
  }else{//不为0的话就直接用全局的值
    param["user_id"] = app.globalData.user_id;
    param["user_token"] = app.globalData.user_token;
  }
  console.log(param)
  wx.request({
    url: baseUrl,
    data: param,
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    method: 'POST',
    success: function (res) {
      success(res)
    },
    fail: function (res) {
      wx.showToast({
        title: '网络请求失败，请退出重试',
        icon: 'none'
      })
    }
  })
}

module.exports = {
  requestHttp: requestHttp,
  requestPostHttp: requestPostHttp,
}
function alertTItle(){
  wx.showLoading({
    title: '正在加载',
    mask: true,
    success: function (res) { },
    fail: function (res) { },
    complete: function (res) {
      setTimeout(function (e) {
        wx.hideLoading();
      }, 1500);
    },

  })
}
